package com.example.demo.cotroller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.pojo.LoginUser;
import com.example.demo.pojo.SysUser;
import com.example.demo.service.SysUserService;
import com.example.demo.uilt.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.http.SecurityHeaders;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api
@ApiModel("用户")
@Controller
public class SysUserCotroller {

  @Autowired
private SysUserService sysUserService;

//  @ApiOperation("登录")
//  @ResponseBody
//  @RequestMapping("/dl")
//  public ResponseResult doLogin(@ApiParam("账号")String login_name,
//  @ApiParam("密码")String password){
//    QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
//    queryWrapper.eq("login_name",login_name);
//    queryWrapper.eq("password",password);
//    SysUser one = sysUserService.getOne(queryWrapper);
//    if(one==null){
//      return new ResponseResult(500,"登录失败",one);
//    }else {
//      return new ResponseResult(200,"登录成功",one);
//    }
//  }

  @ApiOperation("登录")
  @ResponseBody
  @PostMapping("/dl2")
  public ResponseResult doLogin2(@RequestBody @ApiParam("账号xixni")SysUser sysUser){
//
    return sysUserService.login(sysUser);
//    QueryWrapper<SysUser> userQueryWrapper=new QueryWrapper<>();
//    userQueryWrapper.eq("login_name",loginName);
//    userQueryWrapper.eq("password",password);
//    SysUser one = sysUserService.getOne(userQueryWrapper);
//    if(one==null){
//      return new ResponseResult(500,"shibai",one);
//    }
//     new ResponseResult(200,"cg",one);
  }

  @ResponseBody
  @ApiOperation("查询user")
  @PreAuthorize("hasAuthority('system:user:list')")
  @GetMapping("/userList")
  public ResponseResult userLists(@RequestBody SysUser sysUser, Integer pageIndex,Integer pageSize){
 return sysUserService.userLists(sysUser, pageIndex, pageSize);



  }

  @ResponseBody
  @ApiOperation("新增user")
  @PreAuthorize("hasAuthority('system:user:add')")
  @GetMapping("/userInsert")
  public ResponseResult userInsert(@ApiParam("新增的字段")SysUser sysUser){
    boolean save = sysUserService.save(sysUser);
    if(save){
      return new ResponseResult(200,"成功！");
    }else {
      return new ResponseResult(500,"失败！");
    }
  }

  @ResponseBody
  @ApiOperation("删除user")
  @GetMapping("/userDelete")
  public ResponseResult userDelete(@RequestBody@ApiParam ("user_id")SysUser sysUser){
    Long userId = sysUser.getUserId();
    QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
    queryWrapper.eq("user_id",userId);
    boolean remove = sysUserService.remove(queryWrapper);
    if(remove){
      return new ResponseResult(200,"成功！");
    }else {
      return new ResponseResult(500,"失败！");
    }
  }
//修改
  @ResponseBody
  @ApiOperation("修改user")
  @GetMapping("/userUpdate")
  public ResponseResult update(@RequestBody SysUser sysUser){
    Long userId=sysUser.getUserId();
    QueryWrapper<SysUser> queryWrapper=new QueryWrapper<>();
    queryWrapper.eq("user_id",userId);
    boolean update = sysUserService.update(sysUser, queryWrapper);
    if(update){
      return new ResponseResult(200,"成功",userId);
    }else {
      return new ResponseResult(500,"失败",update);
    }
  }
}
